//
//  SortTestTool.h
//  ALG
//
//  Created by Chenxuhun on 17/4/17.
//  Copyright © 2017年 Chenxuhun. All rights reserved.
//

#ifndef SortTestTool_h
#define SortTestTool_h

#include <iostream>
#include <cassert>

namespace SortTestTool {
    
    
    int * generateRandomArray(int n,int rangeL,int rangeR){
        
        assert (rangeL<=rangeR);
        
        int *arry = new int [n];
        unsigned rt = (unsigned)std::time(NULL);
        srand(rt);
        for (int i = 0; i<n; i++) {
            arry[i] = rand() % (rangeR-rangeL + 1)+ rangeL;
        }
        return arry;
        
    }
    
    int * generateNearlyOrderedArray(int n,int randCount){
        
        int *array = new int[n];
        for (int i = 0; i<n; i++) {
            
            array[i] = i;
        }
        unsigned rt = (unsigned)std::time(NULL);
        srand(rt);
        
        for (int j = 0; j<randCount; j++) {
            int x = rand() % n;
            int y = rand() % n;
            std::swap(array[x], array[y]);
        }
      
        
        return array;
        
    }
    
    template<typename T>
    void printArray(T arr[],int n){
        
        for (int i = 0; i<n; i++) {
            std::cout<<arr[i]<<"  ";
        }
        std::cout<<std::endl;
    }
    template<typename T>
    bool isSorted (T arr[],int n){
        
        for (int i = 0; i< n-1; i++) {
            if (arr[i]>arr[i+1]) {
                return false;
            }
        }
        return true;
    }
    
    template<typename T>
    void testSort(std::string name,void(*sort)(T[],int),T arr[],int n){
        
        clock_t startTime = clock();
        sort(arr,n);
        clock_t endTime = clock();
        assert(isSorted(arr, n));
        std::cout<<name<<":"<<double(endTime-startTime)/CLOCKS_PER_SEC<< "s" << std::endl;
    }
    
    int * copyArray(int array[],int n){
        
        int *temp = new int[n];
        
        std::copy(array, array+n, temp);
        
        
        return temp;
        
    }
    
    
}


#endif /* SortTestTool_h */
